package com.log.james.controller;

import com.log.james.service.UserService;
import com.log.james.utils.JsonUtil;
import com.log.james.utils.PwdEnCoder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录的处理界面
 */
@Api(tags = "登录接口")
@RestController
public class LoginController {

    private JsonUtil jsonUtil = new JsonUtil();
    private PwdEnCoder pwdEnCoder = new PwdEnCoder();
    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
//        获取姓名
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        logger.info("传参的用户名为： " + username );
//        对用户名进行哈希加盐
        String salt = pwdEnCoder.encode(Long.parseLong(username));
        String hashSalt = pwdEnCoder.getMD5(salt);
//        通过用户名哈希盐和前段传输的密码生成密文
        String ecPassWord = new SimpleHash("SHA-1",password,hashSalt).toString();

        String pwd = userService.findPassword(username);
//        通过用户名获取密码
        if (pwd.equals(ecPassWord)){}
        else{}

        Map<String,Object> result = new HashMap<>();
        return jsonUtil.mapToJson(result);
    }

//    @ApiOperation("使用json数据用户登录")
//    @PostMapping("/loginJson")
//    public
}
